//
//  AKUIHumanPlayer.m
//  AKUI
//
//  Created by Ben Chen on 3/8/08.
//  Copyright 2008 Sleeper. All rights reserved.
//

#import "AKUIRenderView.h"
#import "AKUIHumanPlayer.h"

@interface AKUIHumanPlayer ()

@property (assign) AKMove	bestMove;

@end

@implementation AKUIHumanPlayer

- (id) initWithSide:(AKSideIndex)aSide 
	   numOfPlayers:(unsigned)playersCount {
	self = [super init];
	if (self != nil) {
		self.side = aSide;
	}
	return self;
}

@synthesize interactiveDrawing, stateMaintainer, bestMove, side;

- (BOOL)canBecomeFirstResponder {
	return YES;
}

- (BOOL)becomeFirstResponder {
	return YES;
}

- (BOOL)canResignFirstResponder {
	return YES;
}

- (BOOL)resignFirstResponder {
	return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 1 && [[event allTouches] count] == 1) {
		UITouch *touch = [touches anyObject];
		CGPoint pointInView = [touch locationInView:interactiveDrawing.drawingView];
		AKCellIndex cellIndex = [interactiveDrawing cellIndexForPoint:pointInView];
		NSLog(@"point:x:%f y:%f index:%d", pointInView.x, pointInView.y, cellIndex);
		AKPiece moveablePiece = [stateMaintainer moveablePieceAtCell:cellIndex];
		
		if (AKEqualPieces(moveablePiece, AKZeroPiece)) {
			// There's no piece that can be moved
			// Then we retrieve what piece can be made here
			NSArray *newPieces = [stateMaintainer newPiecesAtCell:cellIndex];
			
			if (newPieces) {
				AKMove newMove;
				newMove.piece	= [[newPieces objectAtIndex:0] unsignedIntValue];	// TODO: there should be a way to let the user decide which piece to make
				newMove.side	= self.side;
				newMove.srcCell	= NSNotFound;
				newMove.dstCell	= cellIndex;
				[interactiveDrawing.drawingView setNextResponder:nil];
				self.bestMove = newMove; // should trigger observer's method
			}
			currentTouch = nil;
		} else {
			[interactiveDrawing activatePiece:moveablePiece];	// make sure it's activated before dragging
			[interactiveDrawing aboutToDragPiece:moveablePiece atPoint:pointInView];
			currentTouch = touch;
			draggedPiece = moveablePiece;
		}
	}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 1 && [[touches anyObject] isEqual:currentTouch]) {
		CGPoint pointInView = [currentTouch locationInView:interactiveDrawing.drawingView];
		[interactiveDrawing dragPieceAt:pointInView];
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 1 && [[touches anyObject] isEqual:currentTouch]) {	
		[interactiveDrawing deactivatePiece];
		CGPoint pointInView = [currentTouch locationInView:interactiveDrawing.drawingView];
		AKCellIndex dstCell = [interactiveDrawing cellIndexForPoint:pointInView];
		if (dstCell != NSNotFound) {
			NSArray *dstArray = [stateMaintainer reachableCellsForPiece:draggedPiece];
			if ([dstArray containsObject:[NSNumber numberWithUnsignedInt:dstCell]]) {
				[interactiveDrawing stopDraggingShouldComeBack:NO];
				
				AKMove newMove;
				newMove.piece = draggedPiece.piece;
				newMove.srcCell	= draggedPiece.cell;
				newMove.dstCell	= dstCell;
				newMove.side	= draggedPiece.side;
				
				[interactiveDrawing.drawingView setNextResponder:nil];
				self.bestMove = newMove;
			} else {
				[interactiveDrawing stopDraggingShouldComeBack:YES];
			}
		}
		else {
			[interactiveDrawing stopDraggingShouldComeBack:YES];
		}
	}		
}

#pragma mark AKPlayer methods

- (void)backToInitialState {
}

- (void)thinkNextMove {
	[(interactiveDrawing.drawingView) setNextResponder:self];
}

- (BOOL)performMove:(AKMove)newMove {
	return YES;	// Since humen should "maintain" the state themselves
}

- (NSString*)gameName {
	return AKPlugInAnyGame;
}

- (void)setStateWithStateSaver:(NSArray*)anArray {
}

@end
